package com.lanou3g.ptp.api.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.lanou3g.ptp.base.service.BaseService;
import com.lanou3g.ptp.entity.Base;
import com.lanou3g.ptp.utils.PtpResult;
import com.lanou3g.ptp.utils.PtpResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(description = "基地接口")
@RestController
public class BaseController {
    @Reference
    private BaseService baseService;

    @ApiOperation("查询所有基地")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数"),
            @ApiImplicitParam(name = "size", value = "数量"),
            @ApiImplicitParam(name = "keyword", value = "关键字")
    })
    @GetMapping("/bases")
    public PtpResult findAll(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "10") Integer size,
                             String keyword) {
        PageInfo<Base> all;
        try {
            if (keyword != null && !keyword.trim().equals("")) {
                all = baseService.findByKeyword(page, size, keyword);
            } else {
                all = baseService.findAll(page, size);
            }
            return PtpResultUtils.ok(all);
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }

    }


    @ApiOperation("添加基地")
    @PostMapping("/base")
    public PtpResult insert(@RequestBody Base base) {
        try {
            baseService.insert(base);
            return PtpResultUtils.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }
    }

    @ApiOperation("删除单个基地")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "基地id")
    })
    @DeleteMapping("/base/{id}")
    public PtpResult delete(@PathVariable Long id) {
        try {
            baseService.deleteById(id);
            return PtpResultUtils.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }
    }

    @ApiOperation("删除多个基地")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "基地id数组")
    })
    @DeleteMapping("/bases/{ids}")
    public PtpResult deletes(@PathVariable Long[] ids) {
        try {
            baseService.deleteByIds(ids);
            return PtpResultUtils.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }
    }

    @ApiOperation("修改基地")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "基地id")
    })
    @PutMapping("/base/{id}")
    public PtpResult update(@PathVariable Long id,@RequestBody Base base) {
        try {
            baseService.updateById(base);
            return PtpResultUtils.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }
    }

    @ApiOperation("关键词查询不带参")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "基地id")
    })
    @GetMapping("/bases1")
    public PtpResult findByName(String keyword){
        try{
            List<Base> byName = baseService.findByName(keyword);
            return PtpResultUtils.ok(byName);
        }catch (Exception e){
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }

    }

    /**
     * @author 武奇
     * @date 2019-08-06 21:25:27
     * @param baseId 基地ID
     * @return 基地详情
     */
    @ApiOperation("根据基地id查询基地详情")
    @GetMapping("/base/{baseId}")
    public PtpResult findOne(@PathVariable Long baseId) {
        Base base = this.baseService.findById(baseId);
        return PtpResultUtils.ok(base);
    }
}
